﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;

namespace IGuess
{
    public partial class FrequenceForm : Form
    {
        ClassifierForm form;
        FrequenceClassifier classifier;
        private string fileName;

        public FrequenceForm(ClassifierForm form, FrequenceClassifier classifier, string fileName)
        {
            InitializeComponent();
            this.form = form;
            this.classifier = classifier;
            this.fileName = fileName;
        }

        private void FrequenceForm_Load(object sender, EventArgs e)
        {
            this.frequenceFormAcceptButton.Text = RM.GetString("frequenceFormAcceptButton");
            this.frequenceFormFrequenceWordLabel.Text = RM.GetString("frequenceFormFrequenceWordLabel");
            this.frequenceFormResultLabel.Text = RM.GetString("frequenceFormResultLabel");
            this.frequenceFormTotalWordsLabel.Text = RM.GetString("frequenceFormTotalWordsLabel");
            this.frequenceFormResultTextBox.Text = this.classifier.Classify();
            this.frequenceFormTotalWordsTextBox.Text = this.classifier.NumberOfWords.ToString();
            this.frequenceFormMostFrequentWordTextBox.Text = this.classifier.MostOccurentWord;
            this.writeStats();
        }

        private void frequenceFormAcceptButton_Click(object sender, EventArgs e)
        {
            this.form.Enabled = true;
            this.Close();
        }

        public void writeStats()
        {
            string[,] statsFile = new string[3, 4];

            if (File.Exists(ConfigurationManager.AppSettings["StatisticsFile"]))
            {

                //Read the file and store its content
                using (System.IO.StreamReader sr = new System.IO.StreamReader(ConfigurationManager.AppSettings["StatisticsFile"]))
                {
                    string line;
                    int i = 0;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] token = line.Split();
                        statsFile[i, 0] = token[0];
                        statsFile[i, 1] = token[1];
                        statsFile[i, 2] = token[2];
                        statsFile[i, 3] = token[3];
                        i++;
                    }

                }

                if (this.fileName.ToLower().Contains("en") && classifier.Result.ToLower().Equals("en"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[0, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[0, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[0, 3]);

                    statsFile[0, 1] = (totalENTexts + 1).ToString();
                    statsFile[0, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("de") && classifier.Result.ToLower().Equals("de"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[1, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[1, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[1, 3]);

                    statsFile[1, 1] = (totalENTexts + 1).ToString();
                    statsFile[1, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("fr") && classifier.Result.ToLower().Equals("fr"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[2, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[2, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[2, 3]);

                    statsFile[2, 1] = (totalENTexts + 1).ToString();
                    statsFile[2, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else
                {

                    if (this.fileName.ToLower().Contains("en"))
                    {

                        int totalENTexts = Convert.ToInt32(statsFile[0, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[0, 3]);

                        statsFile[0, 1] = (totalENTexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("de"))
                    {

                        int totalDETexts = Convert.ToInt32(statsFile[0, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[1, 3]);

                        statsFile[1, 1] = (totalDETexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("fr"))
                    {

                        int totalENTexts = Convert.ToInt32(statsFile[2, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[2, 3]);

                        statsFile[2, 1] = (totalENTexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }

                }


            }
            else
            {

                if (this.fileName.ToLower().Contains("en") && classifier.Result.ToLower().Equals("en"))
                {

                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 1.ToString();
                    statsFile[0, 2] = 1.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 0.ToString();
                    statsFile[1, 2] = 0.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 0.ToString();
                    statsFile[2, 2] = 0.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("de") && classifier.Result.ToLower().Equals("de"))
                {


                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 0.ToString();
                    statsFile[0, 2] = 0.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 1.ToString();
                    statsFile[1, 2] = 1.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 0.ToString();
                    statsFile[2, 2] = 0.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("fr") && classifier.Result.ToLower().Equals("fr"))
                {


                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 0.ToString();
                    statsFile[0, 2] = 0.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 0.ToString();
                    statsFile[1, 2] = 0.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 1.ToString();
                    statsFile[2, 2] = 1.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else
                {

                    if (this.fileName.ToLower().Contains("en"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 1.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 0.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 0.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("de"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 0.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 1.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 0.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("fr"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 0.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 0.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 1.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }

                }

            }


        }

        private void BayesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.form.Enabled = true;
        }

    }
}
